/*
 * Lattice.java
 *
 * Created on September 14, 2006, 2:41 PM
 *
 * (C) 2006 R. Alexander Milowski
 */

package com.smallx.xproc.compiler.graph;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author alex
 */
public class Graph extends HashSet<Edge>  {
   
   Map<String,Vertex> vertices;
   Map<Vertex,Set<Edge>> inbound;
   String id;
   /** Creates a new instance of Lattice */
   public Graph(String id) {
      this.id = id;
      this.vertices = new HashMap<String,Vertex>();
      this.inbound = null;
   }
   
   public String getId() {
      return id;
   }
   
   public void index() {
      inbound = new HashMap<Vertex,Set<Edge>>();
      for (Edge e : this) {
         if (e.isDirected()) {
            Set<Edge> edges = inbound.get(e.getTarget());
            if (edges==null) {
               edges = new HashSet<Edge>();
               inbound.put(e.getTarget(),edges);
            }
            edges.add(e);
         }
      }
   }
   
   public Set<Edge> getInbound(Vertex v) {
      return inbound.get(v);
   }
   
   public Map<String,Vertex> getVertexMap() {
      return vertices;
   }
   
   public Collection<Vertex> getVertices() {
      return vertices.values();
   }
   
   public Vertex getVertex(String id) {
      return vertices.get(id);
   }
   
   public void addVertex(Vertex v) {
      if (vertices.get(v.getId())!=null) {
         throw new IllegalArgumentException("A vertex with id "+v.getId()+" already exists.");
      }
      vertices.put(v.getId(),v);
   }
   
}
